package com.pdwl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pdwl.common.dtos.PageRequestDto;
import com.pdwl.common.dtos.PageResponseResult;
import com.pdwl.common.dtos.ResponseResult;
import com.pdwl.common.enums.AppHttpCodeEnum;
import com.pdwl.common.exception.CustomException;
import com.pdwl.common.vo.TransportOrderVo;
import com.pdwl.mapper.TaskTransportMapper;
import com.pdwl.common.vo.TaskTransportVo;
import com.pdwl.service.TaskTransportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.TypeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.sql.SQLException;
import java.util.List;

/**运单管理业务处理
 * @author Thd
 * @date 2023/1/11 10:24
 */
@Service
@Transactional
@Slf4j
public class TaskTransportServiceImpl implements TaskTransportService {
    @Autowired
    private TaskTransportMapper taskTransportMapper;
    @Override
    public PageResponseResult findAll(PageRequestDto<TaskTransportVo> pageRequestDto) {
        //1.数据校验
        if (ObjectUtils.isEmpty(pageRequestDto.getPage()) || ObjectUtils.isEmpty(pageRequestDto.getSize())) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID );//自定异常
        }

        PageHelper.startPage(pageRequestDto.getPage(), pageRequestDto.getSize());
        //2、业务处理
        TaskTransportVo requestDto = pageRequestDto.getEntity();
        List<TaskTransportVo> tasks =taskTransportMapper.findAll(requestDto);


        if (ObjectUtils.isEmpty(tasks)&&tasks.size()==0){
            throw new CustomException(AppHttpCodeEnum.DATA_NOT_EXIST );
        }
        //3、数据封装
        PageInfo<TaskTransportVo> pageInfo=new PageInfo<>(tasks);
        PageResponseResult<List<TaskTransportVo>> responseResult = PageResponseResult.success(pageInfo);

        return responseResult;
    }


    //运单详情显示
    @Override
    public TransportOrderVo detail(String orderId) {
        //参数校验
        if (StringUtils.isBlank(orderId)) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //业务处理
        TransportOrderVo transportOrderVo =taskTransportMapper.detail(orderId);
        //数据封装
        return transportOrderVo;
    }
}
